import { FormSchema } from "/@/components/Form/index"
import { rules } from "/@/utils/helper/validator"
import anquan1 from "./icons/anquan1.png"
import anquan2 from "./icons/anquan2.png"
import app1 from "./icons/app1.png"
import app2 from "./icons/app2.png"
import geren1 from "./icons/geren1.png"
import geren2 from "./icons/geren2.png"
import zuhu1 from "./icons/zuhu1.png"
import zuhu2 from "./icons/zuhu2.png"
import operageLog1 from "./icons/operageLog1.png"
import operageLog2 from "./icons/operageLog2.png"
import loginLog1 from "./icons/loginLog1.png"
import loginLog2 from "./icons/loginLog2.png"
import { calculateFileSize } from "/@/utils/common/compUtils"
import { BasicColumn } from "@/components/Table"
import { render } from "/@/utils/common/renderUtils"
import { DescItem } from "/@/components/Description/index"
import dictEnum from "/@/enums/dictEnum"

/**
 * 调用后端接口授权的状态
 */
export enum AuthStateEnum {
  LOGIN_SUCCESS = 1,
  BIND_USER = 2,
  SCAN_DOING = 10,
  SCAN_FINISH = 11,
  SCAN_TIMEOUT = 12
}


export interface ListItem {
  key: string
  title: string
  description: string
  extra?: string
  avatar?: string
  color?: string
}

// tab的list
export const settingList = [
  {
    key: "1",
    name: "个人信息",
    component: "BaseSetting",
    icon: "ant-design:user-outlined",
    img1: geren1,
    img2: geren2,
  },
  {
    key: "2",
    name: "我的企业",
    component: "TenantSetting",
    isSlot: false,
    icon: "ant-design:team-outlined",
    img1: zuhu1,
    img2: zuhu2,
  },
  {
    key: "3",
    name: "账号安全",
    component: "AccountSetting",
    icon: "ant-design:lock-outlined",
    img1: anquan1,
    img2: anquan2,
  },
  {
    key: "4",
    name: "账号绑定",
    component: "WeChatDingSetting",
    icon: "ant-design:contacts-outlined",
    img1: app1,
    img2: app2,
  },
  {
    key: "5",
    name: "操作日志",
    component: "OperateLog",
    icon: "ant-design:contacts-outlined",
    img1: operageLog1,
    img2: operageLog2,
  },
  {
    key: "6",
    name: "登录日志",
    component: "LoginLog",
    icon: "ant-design:contacts-outlined",
    img1: loginLog1,
    img2: loginLog2,
  },
]

export const operateLogColumns: BasicColumn[] = [
  {
    title: "traceId",
    dataIndex: "traceId",
    width: 140,
    align: "left",
  },
  {
    title: "操作地址",
    dataIndex: "userIp",
    width: 130,
  },
  {
    title: "业务类型",
    dataIndex: "typeName",
    width: 120,
    customRender: ({ text }) => {
      return text ? text : "-"
    },
  },
  {
    title: "业务子类型",
    dataIndex: "subTypeName",
    width: 140,
    customRender: ({ text }) => {
      return text ? text : "-"
    },
  },
  {
    title: "操作内容",
    dataIndex: "action",
    width: 150,
    customRender: ({ text }) => {
      return text ? text : "-"
    },
  },
  {
    title: "状态",
    dataIndex: "status",
    width: 60,
    customRender: ({ text }) => {
      const color = text ? "green" : "red"
      return render.renderTag(text ? '成功' : '失败', color)
    },
  },
  {
    title: "操作日期",
    dataIndex: "createdAt",
    width: 150,
  },
]

export const operateLogSearchFormSchema: FormSchema[] = [
  {
    field: "logTypeId",
    label: "业务",
    component: "MyTreeSelect",
    colProps: { span: 8 },
  },
  {
    field: "traceId",
    label: "traceId",
    component: "Input",
    colProps: { span: 8 },
  },
  {
    field: "status",
    label: "状态",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "access_status",
      stringToNumber: true,
    },
    colProps: { span: 8 },
  },
  {
    field: "createdAt",
    component: "RangePicker",
    label: "操作时间",
    colProps: {
      span: 8,
    },
  },
]

export const operateLogformDescSchema = [
  {
    field: "traceId",
    label: "请求ID",
  },
  {
    field: "costTime",
    label: "耗时",
    render: (curVal, data) => {
      const color = curVal >= 500 ? "red" : "green"
      return render.renderTag(curVal + " ms", color)
    },
  },
  {
    field: "typeName",
    label: "业务类型",
  },
  {
    field: "subTypeName",
    label: "子类型",
  },
  {
    field: "bizId",
    label: "业务编号",
  },
  {
    field: "userIp",
    label: "操作者IP",
  },
  {
    field: "location",
    label: "操作地点",
  },
  {
    field: "userName",
    label: "操作人员",
  },
  {
    field: "action",
    label: "操作内容",
  },
  {
    field: "requestMethod",
    label: "请求方式",
  },
  {
    field: "requestUrl",
    label: "请求url",
  },
  {
    field: "status",
    label: "操作状态",
    render: (curVal, data) => {
      const color = curVal ? "green" : "red"
      return render.renderTag(curVal ? '成功' : '失败', color)
    },
  },
  {
    field: "createdAt",
    label: "操作日期",
  }
  
]

export const loginLogColumns: BasicColumn[] = [
  {
    title: "登录IP",
    dataIndex: "ip",
    width: 140,
  },
  {
    title: "登录地点",
    dataIndex: "location",
    width: 150,
  },
  {
    title: "浏览器",
    dataIndex: "browser",
    width: 100,
  },
  {
    title: "操作系统",
    dataIndex: "os",
    width: 100,
  },

  {
    title: "状态",
    dataIndex: "status",
    width: 60,
    customRender: ({ text }) => {
      const color = text == "2" ? "red" : text == "1" ? "green" : "gray"
      return render.renderTag(render.renderDict(text, dictEnum.LoginLogStatus), color)
    },
  },
  {
    title: "操作信息",
    dataIndex: "msg",
    width: 120,
  },
  {
    title: "登录日期",
    dataIndex: "loginTime",
    width: 150,
  },
]

export const loginLogFormDetailSchema: DescItem[] = [
  {
    field: "userName",
    label: "用户名",
  },
  {
    field: "terminalName",
    label: "终端",
  },
  {
    field: "grantTypeName",
    label: "授权方式",
  },
  {
    field: "ip",
    label: "登录IP",
  },
  {
    field: "location",
    label: "登录地点",
  },
  {
    field: "browser",
    label: "浏览器",
  },
  {
    field: "os",
    label: "操作系统",
  },
  {
    field: "status",
    label: "状态",
    render: (curVal, data) => {
      const color = curVal == "2" ? "red" : curVal == "1" ? "green" : "gray"
      return render.renderTag(render.renderDict(curVal, dictEnum.LoginLogStatus), color)
    },
  },
  {
    field: "msg",
    label: "操作信息",
  },
  {
    field: "loginTime",
    label: "登录日期",
  },
]

export const loginLogSearchFormSchema: FormSchema[] = [
  {
    field: "status",
    label: "登录状态",
    component: "JDictSelectTag",
    componentProps: {
      dictType: dictEnum.LoginLogStatus,
      stringToNumber: true,
    },
    colProps: { span: 8 },
  },
  {
    field: "loginTime",
    component: "RangePicker",
    label: "登录时间",
    componentProps: {
      valueType: "Date",
    },
    colProps: {
      span: 8,
    },
  },
]

/**
 * 用户表单
 */
export const formSchema: FormSchema[] = [
  {
    field: "realName",
    component: "Input",
    label: "姓名",
    colProps: { span: 24 },
    required: true,
  },
  {
    field: "birthday",
    component: "DatePicker",
    label: "生日",
    colProps: { span: 24 },
    componentProps: {
      showTime: false,
      valueFormat: "YYYY-MM-DD",
      getPopupContainer: () => document.body,
    },
  },
  {
    field: "sex",
    component: "RadioGroup",
    label: "性别",
    colProps: { span: 24 },
    componentProps: {
      options: [
        {
          label: "男",
          value: "1",
        },
        {
          label: "女",
          value: "2",
        },
      ],
    },
  },
  {
    field: "relTenantIds",
    component: "JDictSelectTag",
    label: "租户",
    colProps: { span: 24 },
    componentProps: {
      mode: "multiple",
      dictType: "sys_tenant,name,id",
      disabled: true,
    },
  },
  {
    field: "post",
    component: "JDictSelectTag",
    label: "职位",
    colProps: { span: 24 },
    componentProps: {
      mode: "multiple",
      dictType: "sys_position,name,id",
      disabled: true,
    },
  },
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
]

//密码弹窗
export const formPasswordSchema: FormSchema[] = [
  {
    label: "用户账号",
    field: "username",
    component: "Input",
    componentProps: { readOnly: true },
  },
  {
    label: "旧密码",
    field: "oldpassword",
    component: "InputPassword",
    required: true,
  },
  {
    label: "新密码",
    field: "password",
    component: "StrengthMeter",
    componentProps: {
      placeholder: "请输入新密码",
    },
    rules: [
      {
        required: true,
        message: "请输入新密码",
      },
    ],
  },
  {
    label: "确认新密码",
    field: "confirmpassword",
    component: "InputPassword",
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
]
